package org.jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {

        /**
         * 先创建一个MBeanServer，相当于一个MBean的容器。然后创建一个MBean(User)，
         * 并给它取名为jmx:type=User，然后将该MBean注册到MBeanServer即可。
         */
        MBeanServer server= ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName=new ObjectName("jmx:type=User");
        User bean=new User();
        server.registerMBean(bean, objectName);
        String oldName=null;
        String oldPwd=null;
        System.out.println("jmx started!!!");
        while(true){
            if(oldName!=bean.getName()|| oldPwd !=bean.getPasswd()){
                System.out.println(bean.getName()+":"+bean.getPasswd());
                oldName=bean.getName();
                oldPwd=bean.getPasswd();
            }
            Thread.sleep(1000);
        }

    }
}
